<?php

use kzo\base\widgets\ui\common\grid\GridView;
use yii\helpers\Html;
use yii\helpers\Url;
use kzo\base\models\statics\EntityModelStatus;
use kzo\base\models\statics\OperationEvent;
use kzo\base\widgets\ui\common\grid\ActionColumn;

/* @var $this yii\web\View */
/* @var $searchModel common\models\c2\search\CardHoldSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */
/* @var $model common\models\c2\search\CardHoldSearch */

$this->title = Yii::t('app.c2', 'Card Holds');
$this->params['breadcrumbs'][] = $this->title;
$user_id = Yii::$app->request->queryParams['CardHoldSearch']['user_id'];
$user = \common\models\c2\entity\FeUser::findOne($user_id);
?>
<div class="card-hold-index">
    <p style="color: red;">如持卡状态显示未过期，实际按会员卡到期时间为准</p>

    <?php // echo $this->render('_search', ['model' => $searchModel]); ?>

    <?php echo GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,

        'pjax' => true,
        'hover' => true,
        'showPageSummary' => true,
        'panel' => ['type' => GridView::TYPE_SUCCESS, 'heading' => $user->username . "的会员卡"],
        'toolbar' => [
            [
                'content' =>
                    Html::a('<i class="fas fa-plus"></i>', ['edit'], [
                        'class' => 'btn btn-success',
                        'title' => Yii::t('app.c2', 'Add'),
                        'data-pjax' => '0',
                    ]) . ' ' .
                    Html::button('<i class="fas fa-minus"></i>', [
                        'class' => 'btn btn-danger',
                        'title' => Yii::t('app.c2', 'Delete Selected Items'),
                        'onClick' => "jQuery(this).trigger('" . OperationEvent::DELETE_BY_IDS . "', {url:'" . Url::toRoute('multiple-delete') . "'});",
                    ]) . ' ' .
                    Html::a('<i class="fas fa-redo"></i>', Url::current(), [
                        'class' => 'btn btn-outline-secondary',
                        'title' => Yii::t('app.c2', 'Reset Grid')
                    ]),
                'options' => ['class' => 'btn-group mr-2']
            ],
            '{export}',
            '{toggleData}',
        ],
        'exportContainer' => ['class' => 'btn-group mr-2'],
        'exportConfig' => [],
        'columns' => [
            ['class' => 'kartik\grid\CheckboxColumn'],
            ['class' => 'kartik\grid\SerialColumn'],
            [
                'class' => 'kartik\grid\ExpandRowColumn',
                'value' => function ($model, $key, $index, $column) {
                    return GridView::ROW_COLLAPSED;
                },
                'detail' => function ($model, $key, $index, $column) {
                    return Yii::$app->controller->renderPartial('_detail', ['model' => $model]);
                },
            ],
            // 'id',
            // 'user_id',
            [
                'attribute' => 'card_id',
                'value' => function ($model) {
                    return $model->card->name;
                }
            ],
            [
                'attribute' => 'buy_at',
                'value' => function ($model) {
                    // return date("Y-m-d", strtotime($model->buy_at));
                    return $model->buy_at;
                }
            ],
            // 'buy_at',
            // 'expired_at',
            [
                'attribute' => 'expired_at',
                'value' => function ($model) {
                    // return date("Y-m-d", strtotime($model->expired_at));
                    return $model->expired_at;
                }
            ],
            'usage_time',
            'remain_time',
            // 'status',
            // 'created_at',
            // 'updated_at',
            [
                'attribute' => 'status',
                'class' => '\kartik\grid\EditableColumn',
                'editableOptions' => [
                    'inputType' => \kartik\editable\Editable::INPUT_DROPDOWN_LIST,
                    'formOptions' => ['action' => Url::toRoute('editColumn')],
                    'data' => \common\models\c2\statics\CardStatus::getHashMap('id', 'label'),
                    'displayValueConfig' => \common\models\c2\statics\CardStatus::getHashMap('id', 'label'),
                ],
                'filter' => EntityModelStatus::getHashMap('id', 'label'),
                'value' => function ($model) {
                    return \common\models\c2\statics\CardStatus::getLabel($model->status);
                }
            ],
            [
                'class' => ActionColumn::className(),
                'template' => '{update} {delete} {lesson-apply}',
                'width' => '150px',
                'buttons' => [
                    'update' => function ($url, $model, $key) {
                        return Html::a('<span class="fas fa-pencil-alt"></span>', ['edit', 'id' => $model->id], [
                            'title' => Yii::t('app', 'Info'),
                            'data-pjax' => '0',
                        ]);
                    },
                    'lesson-apply' => function ($url, $model, $key) {
                        return Html::a(Yii::t('app.c2', 'Lesson Apply Record'),
                            ['/lesson-apply/default/index', 'LessonApplySearch[card_hold_id]' => $model->id], [
                                'class' => 'btn btn-warning btn-sm',
                                'style' => 'font-size:12px;',
                                'title' => Yii::t('app.c2', 'Lesson Apply Record'),
                                'data-pjax' => '0',
                            ]);
                    },
                ]
            ],

        ],
    ]); ?>

</div>
